package com.xc.hotelmgr.consumermq.service.mq.Impl;

import com.xc.hotelmgr.consumermq.constant.MqConstant;
import com.xc.hotelmgr.consumermq.service.mq.MQSetService;
import lombok.RequiredArgsConstructor;
import org.springframework.amqp.rabbit.listener.DirectMessageListenerContainer;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;


@Service
@RequiredArgsConstructor
public class MQSetServiceImpl implements MQSetService {

    private final DirectMessageListenerContainer sendAll;

    private final DirectMessageListenerContainer upDown;

    private final DirectMessageListenerContainer htPull;

    private final DirectMessageListenerContainer htCallback;

    private final DirectMessageListenerContainer rpCallback;

    private final DirectMessageListenerContainer ratesCallback;

    private final DirectMessageListenerContainer MQ2MQHotel;

    private final DirectMessageListenerContainer MQ2MQRp;

    private final DirectMessageListenerContainer MQ2MQRates;

    private final DirectMessageListenerContainer DelRp;

    private final DirectMessageListenerContainer HotelIdListAll;

    private final DirectMessageListenerContainer CMQ2MQRates;


    @Override
    @Transactional
    public void update(Map<String, Object> map) {
        Set<String> set =  map.keySet();
        Object[] list =  set.toArray();
        for (Object s : list) {
            String queue = s.toString();
            updateConsumer(queue, (Integer) map.get(queue));
        }
    }

    @Override
    @Transactional
    public void add(String appKey) {
        addQueue(appKey);
    }

    @Override
    @Transactional
    public void empty(String appKey) {

    }


    /**
     * 更新消费者数量
     */
    private void updateConsumer(String queue, Integer num) {

        if("sendAll".equals(queue)){
            sendAll.setConsumersPerQueue(num);
        }
        if("upDown".equals(queue)) {
            upDown.setConsumersPerQueue(num);
        }
        if("htPull".equals(queue)) {
            htPull.setConsumersPerQueue(num);
        }
        if("htCallback".equals(queue)) {
            htCallback.setConsumersPerQueue(num);
        }
        if("rpCallback".equals(queue)) {
            rpCallback.setConsumersPerQueue(num);
        }
        if("ratesCallback".equals(queue)) {
            ratesCallback.setConsumersPerQueue(num);
        }
        if("MQ2MQHotel".equals(queue)) {
            MQ2MQHotel.setConsumersPerQueue(num);
        }
        if("MQ2MQRp".equals(queue)) {
            MQ2MQRp.setConsumersPerQueue(num);
        }
        if("MQ2MQRates".equals(queue)) {
            MQ2MQRates.setConsumersPerQueue(num);
        }
        if("CMQ2MQRates".equals(queue)) {
            MQ2MQRates.setConsumersPerQueue(num);
        }
        if("delete_rp".equals(queue)) {
            DelRp.setConsumersPerQueue(num);
        }
        if("hotel_id_list".equals(queue)) {
            htPull.setConsumersPerQueue(num);
        }
        if("hotel_id_list_all".equals(queue)) {
            HotelIdListAll.setConsumersPerQueue(num);
        }
        if("xc_change_rates".equals(queue)) {
            CMQ2MQRates.setConsumersPerQueue(num);
        }
    }

    /**
     * 添加队列
     * @param appKey
     * @return
     */
    private void addQueue(String appKey) {
        sendAll.addQueueNames(MqConstant.SEND_ALL.getConstantValue() + appKey);
        upDown.addQueueNames(MqConstant.UPDOWN_HPTEL.getConstantValue() + appKey);
        htCallback.addQueueNames(MqConstant.CALLBACK_HOTEL.getConstantValue() + appKey);
        rpCallback.addQueueNames(MqConstant.CALLBACK_RP.getConstantValue() + appKey);
        ratesCallback.addQueueNames(MqConstant.CALLBACK_RATES.getConstantValue() + appKey);
        DelRp.addQueueNames(MqConstant.DELETE_RP.getConstantValue() + appKey);
    }
}
